/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bar.carta.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bar.carta.model.LanguageDTO;
import com.bar.carta.model.Plato;
import com.bar.carta.model.PlatoDTO;
import com.bar.carta.model.PlatoIdioma;
import com.bar.carta.service.PlatoIdiomaLocalServiceUtil;
import com.bar.carta.service.PlatoLocalServiceUtil;
import com.bar.carta.service.base.PlatoLocalServiceBaseImpl;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ORMException;
import com.liferay.portal.kernel.dao.orm.Order;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * The implementation of the plato local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.bar.carta.service.PlatoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author pgonzalm
 * @see com.bar.carta.service.base.PlatoLocalServiceBaseImpl
 * @see com.bar.carta.service.PlatoLocalServiceUtil
 */
public class PlatoLocalServiceImpl extends PlatoLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.bar.carta.service.PlatoLocalServiceUtil} to access the plato local service.
	 */
	
	private Log log = LogFactoryUtil.getLog(PlatoLocalServiceBaseImpl.class);
	
	public List<Plato> selectPlatos(){
		List<Plato> platos = new ArrayList<Plato>();
		List<Plato> platosAux = new ArrayList<Plato>();
		
		try{
			DynamicQuery dQ = DynamicQueryFactoryUtil.forClass(Plato.class);
			
			Order order = OrderFactoryUtil.asc("platoId");
			dQ.addOrder(order);
			
			platosAux = PlatoLocalServiceUtil.dynamicQuery(dQ);
			
			if (platosAux != null && !platosAux.isEmpty()){
				platos = selectIdiomas(platosAux, null);
			}
		
		} catch (ORMException e) {
			log.error("Error get platos. " + " Error massage: " + e.getMessage());
		} catch (SystemException e) {
			log.error("Error to eject query of get platos. " + " Error massage: " + e.getMessage());
		}
			
		return platos;
	}
	
	public List<Plato> selectPlatosPorTipo(Integer tipo, int idioma){
		List<Plato> platos = new ArrayList<Plato>();
		List<Plato> platosAux = new ArrayList<Plato>();
		
		try{
			DynamicQuery dQ = DynamicQueryFactoryUtil.forClass(Plato.class);
			dQ.add(RestrictionsFactoryUtil.eq("tipoId", tipo));
			Order order = OrderFactoryUtil.asc("platoId");
			dQ.addOrder(order);
			
			platosAux = PlatoLocalServiceUtil.dynamicQuery(dQ);
			
			if (platosAux != null && !platosAux.isEmpty()){
				platos = selectIdiomas(platosAux, idioma);
			}
		
		} catch (ORMException e) {
			log.error("Error get platos por tipo. " + " Error massage: " + e.getMessage());
		} catch (SystemException e) {
			log.error("Error to eject query of get platos por tipo. " + " Error massage: " + e.getMessage());
		}
			
		return platos;
	}
	
	private List<Plato> selectIdiomas(List<Plato> platosAux, Integer idioma){
		List<PlatoIdioma> idiomas = new ArrayList<PlatoIdioma>();
		List<Plato> platos = new ArrayList<Plato>();		
		Collection<Object> platoIds = new ArrayList<Object>();
		
		for (Plato plato : platosAux){
			platoIds.add(plato.getPlatoId());
		}
		try{
			DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(PlatoIdioma.class);
			dynamicQuery.add(RestrictionsFactoryUtil.in("platoId", platoIds));
			if (idioma != null){
				dynamicQuery.add(RestrictionsFactoryUtil.eq("idioma", idioma));
			}
			Order order = OrderFactoryUtil.asc("platoId");
			dynamicQuery.addOrder(order);
			
			idiomas = PlatoIdiomaLocalServiceUtil.dynamicQuery(dynamicQuery);
			
			if (idiomas != null && !idiomas.isEmpty()){
				
				for (Plato platoAux : platosAux){
				
					PlatoDTO plato = new PlatoDTO(platoAux);
					Map<Integer, LanguageDTO> lenguajes = new HashMap<Integer, LanguageDTO>();
					
					for (PlatoIdioma idiomaAux : idiomas){
						LanguageDTO lenguaje = new LanguageDTO();
						lenguaje.setName(idiomaAux.getName());
						lenguaje.setDescription(idiomaAux.getDescripcion());
						lenguaje.setLanguage(idiomaAux.getIdioma());
						lenguajes.put(idiomaAux.getIdioma(), lenguaje);
					}
					
					plato.setLanguages(lenguajes);
					
					platos.add(plato);
				}
			}
		} catch (ORMException e) {
			log.error("Error get idiomas. " + " Error massage: " + e.getMessage());
		} catch (SystemException e) {
			log.error("Error to eject query of get idiomas. " + " Error massage: " + e.getMessage());
		}
		return platos;
	}
	
	public void addUpdatePlato(Plato plato, List<PlatoIdioma> lenguajes){
		Plato platoAux;
		try {
			if (plato.getPlatoId() == 0){
				long platoId = CounterLocalServiceUtil.increment("Plato");
				Plato platoBase = platoPersistence.create(platoId);
				
				platoBase.setActivo(plato.isActivo());
				platoBase.setTipoId(plato.getTipoId());
				platoBase.setCreateDate(new Date());
				platoBase.setPrecio(plato.getPrecio());
				platoBase.setImagen(plato.getImagen());
							
				platoAux = PlatoLocalServiceUtil.updatePlato(platoBase);
			}else{
				platoAux = PlatoLocalServiceUtil.updatePlato(plato);
			}
			for (PlatoIdioma language : lenguajes){
				if (language.getPlatoIdiomaId() == 0){
					long platoIdiomaId = CounterLocalServiceUtil.increment("PlatoIdioma");
					PlatoIdioma platoIdiomaBase = platoIdiomaPersistence.create(platoIdiomaId);
					
					platoIdiomaBase.setName(language.getName());
					platoIdiomaBase.setDescripcion(language.getDescripcion());
					platoIdiomaBase.setIdioma(language.getIdioma());
					platoIdiomaBase.setPlatoId(platoAux.getPlatoId());
					
					PlatoIdiomaLocalServiceUtil.updatePlatoIdioma(platoIdiomaBase);
				}else{
					PlatoIdiomaLocalServiceUtil.updatePlatoIdioma(language);
				}
			}
			
		}catch (SystemException e) {
			log.error("Error to add a plato: " + e.getMessage());
		}
	}
}